<html>
<head><meta charset="utf-8"><title>RecordExprField parse vs ungrammar definition · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/RecordExprField.20parse.20vs.20ungrammar.20definition.html">RecordExprField parse vs ungrammar definition</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="223016877"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/RecordExprField%20parse%20vs%20ungrammar%20definition/near/223016877" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/RecordExprField.20parse.20vs.20ungrammar.20definition.html#223016877">(Jan 17 2021 at 01:15)</a>:</h4>
<p>The ungrammar def for RecordExpreField states<br>
<a href="https://github.com/Veykril/ungrammar/blob/master/rust.ungram#L410-L411">https://github.com/Veykril/ungrammar/blob/master/rust.ungram#L410-L411</a></p>
<div class="codehilite"><pre><span></span><code>RecordExprField =
  Attr* NameRef (&#39;:&#39; Expr)?
</code></pre></div>
<p>Note the <code>Expr</code> being the optional part here,<br>
but the parser in RA parses this with the assumption that the <code>Expr</code> is always there instead:<br>
<a href="https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/parser/src/grammar/expressions.rs#L619-L630">https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/parser/src/grammar/expressions.rs#L619-L630</a></p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">            </span><span class="n">IDENT</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">INT_NUMBER</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="c1">// test_err record_literal_before_ellipsis_recovery</span>
<span class="w">                </span><span class="c1">// fn main() {</span>
<span class="w">                </span><span class="c1">//     S { field ..S::default() }</span>
<span class="w">                </span><span class="c1">// }</span>
<span class="w">                </span><span class="k">if</span><span class="w"> </span><span class="n">p</span><span class="p">.</span><span class="n">nth_at</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="n">T</span><span class="o">!</span><span class="p">[</span>:<span class="p">])</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="n">p</span><span class="p">.</span><span class="n">nth_at</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="n">T</span><span class="o">!</span><span class="p">[</span><span class="o">..</span><span class="p">])</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                    </span><span class="n">name_ref_or_index</span><span class="p">(</span><span class="n">p</span><span class="p">);</span><span class="w"></span>
<span class="w">                    </span><span class="n">p</span><span class="p">.</span><span class="n">expect</span><span class="p">(</span><span class="n">T</span><span class="o">!</span><span class="p">[</span>:<span class="p">]);</span><span class="w"></span>
<span class="w">                </span><span class="p">}</span><span class="w"></span>
<span class="w">                </span><span class="n">expr</span><span class="p">(</span><span class="n">p</span><span class="p">);</span><span class="w"></span>
<span class="w">                </span><span class="n">m</span><span class="p">.</span><span class="n">complete</span><span class="p">(</span><span class="n">p</span><span class="p">,</span><span class="w"> </span><span class="n">RECORD_EXPR_FIELD</span><span class="p">);</span><span class="w"></span>
<span class="w">            </span><span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>Unless I'm missing something the parser should instead always parse the <code>NameRef</code>, but the expression optionally  only no?</p>



<a name="223016963"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/RecordExprField%20parse%20vs%20ungrammar%20definition/near/223016963" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/RecordExprField.20parse.20vs.20ungrammar.20definition.html#223016963">(Jan 17 2021 at 01:17)</a>:</h4>
<p>I noticed this because I was expecting the init shorthand to just be the <code>NameRef</code> but it was an <code>Expr</code> instead.</p>



<a name="223030798"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/RecordExprField%20parse%20vs%20ungrammar%20definition/near/223030798" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/RecordExprField.20parse.20vs.20ungrammar.20definition.html#223030798">(Jan 17 2021 at 08:03)</a>:</h4>
<p>yeah, the ungrammar is wrong</p>



<a name="223030845"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/RecordExprField%20parse%20vs%20ungrammar%20definition/near/223030845" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/RecordExprField.20parse.20vs.20ungrammar.20definition.html#223030845">(Jan 17 2021 at 08:04)</a>:</h4>
<p>label is optional, expression is always there. It is a bit mind bending way to look at it, but it actually simplifies things with the impl</p>



<a name="223030872"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/RecordExprField%20parse%20vs%20ungrammar%20definition/near/223030872" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/RecordExprField.20parse.20vs.20ungrammar.20definition.html#223030872">(Jan 17 2021 at 08:05)</a>:</h4>
<p>and forward compatible with lang team deciding that <code>let foo = Foo { spam, &amp;eggs }</code> is ok</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>